package com.bs.elasticsearch.handler;

import com.bs.common.exception.BizException;
import com.bs.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author bingshao
 * @date 2023/5/10
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 系统异常处理，比如：404,500
     *
     * @param e
     * @return
     * @throws Exception
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    public Result<String> defaultErrorHandler(Exception e) throws Exception {
        log.error("全局异常:{}", e);
        return Result.fail(e.getMessage());
    }

    /**
     * 自定义异常处理
     *
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = BizException.class)
    public Result defaultErrorHandler(BizException e) {
        log.error("全局异常:{}", e);
        return Result.fail(e.getCode(), e.getMessage());
    }
}
